<?php
declare (strict_types=1);

namespace app\admin\service\site\discovery;

use app\admin\logic\site\discovery\Discovery;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * 配置接口
 */
class Config extends BaseService
{

    /**
     * @Inject()
     *
     * @var Discovery
     */
    protected $discovery;

    public function items()
    {
        $data = $this->discovery->getList();

        // 定义字段
        $keyList = [
            'title'  => ListItem::make()->title('名称'),
            'key'    => ListItem::make()->title('标识'),
            'icon'   => ListItem::make('custom')->title('图标'),
            // 'group_title'  => ListItem::make()->title('组别'),
            'status' => ListItem::make('custom')->title('状态'),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;
        // $list->searchKey = $searchKey;
        $list->datas = $data;

        return $list->send();
    }

    /**
     * 编辑元素
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-28
     */
    public function editItem($id)
    {
        if (Request::isPut()) {
            $data = Request::put();
            $item = $this->discovery->editItem($id, $data);

            return $this->parseToData($item->toArray(), 1, '编辑成功');
        }
        $datas         = $this->discovery->with(['icon_url'])->findOrFail($id);
        $discoveryData = $datas->discoveryData;
        if ($discoveryData) {
            $datas->icon     = $discoveryData->icon ?: $datas->icon;
            $datas->icon_url = $discoveryData->icon ? $discoveryData->icon_url : $datas->icon_url;
            $datas->title    = $discoveryData->title ?: $datas['title'];
            $datas->status   = $discoveryData->status ?: 0;
            $datas->sort     = $discoveryData->sort ?: $datas['sort'];
        }

        // $group = app(DiscoveryGroup::class)->field(['id'=>'value','title'])->select()->toArray();
        // 定义字段
        $keyList = [
            'title' => FormItem::make()->title('标题')->required(),
            'icon'  => FormItem::make('custom')->title('图标')->required(),
            // 'group_id'  => FormItem::make('select')->options($group)->title('组别')->required(),
            // 'status'    => FormItem::make('slideSwitch')->title('显示状态')->activeText('显示')->activeValue(1)->inactiveText('隐藏')->inactiveValue(0)->required(),
        ];
        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->datas       = $datas;
        $form->pageKey     = $keyList;
        $form->saveMethod  = 'PUT';
        $form->savePostUrl = '/admin/site/discovery/items/' . $id;

        return $form->send();

    }

    /**
     * 编辑元素字段
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-28
     */
    public function editItemField(int $id, string $field)
    {
        $param = Request::put();
        $item  = $this->discovery->editItemField($id, $field, $param);

        return $this->parseToData($item, 1, '编辑成功');
    }

}
